package com.springboot.example.monitor.util;


import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by guoyao on 14-8-22.
 */
public class DateHelper {
    /**
     * 日期与时间
     */
    public final static String BOTH = "yyyy-MM-dd HH:mm:ss";
    /**
     * 日期
     */
    public final static String DATE = "yyyy-MM-dd";
    /**
     * 时间
     */
    public final static String TIME = "HH:mm:ss";
    /**
     * 一日的毫秒数
     */
    public static long MILLION_SECONDS_OF_DAY = 24 * 60 * 60 * 1000L;// 86400000
    /**
     * 一小时的毫秒数
     */
    public static long MILLION_SECONDS_OF_HOUR = 60 * 60 * 1000L;// 3600000;

    private DateHelper() {

    }

    /**
     * 解析日期字符串
     *
     * @param str 日期串
     * @param fmt 日期格式
     * @return
     */
    public static Date parse(String str, String fmt) {
        SimpleDateFormat simDateFormat = new SimpleDateFormat(fmt);
        Date date = null;
        try {
            date = simDateFormat.parse(str);
        } catch (Exception e) {
        }
        return date;
    }

    /**
     * 格式化日期
     *
     * @param date 日期
     * @param fmt  日期格式
     * @return
     */
    public static String format(Date date, String fmt) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(fmt);
        return simpleDateFormat.format(date);
    }

    /**
     * 计算日期加月
     *
     * @param date
     * @param months
     * @return
     */
    public static Date addMonth(Date date, int months) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, months);
        return c.getTime();
    }

    /**
     * 计算日期加天数
     *
     * @param date
     * @param days
     * @return
     */
    public static Date addDay(Date date, int days) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DAY_OF_MONTH, days);
        return c.getTime();
    }

    /**
     * 计算日期加分钟
     *
     * @param date
     * @param minutes
     * @return
     */
    public static Date addMinutes(Date date, int minutes) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MINUTE, minutes);
        return c.getTime();
    }

    /**
     * 计算天数差
     *
     * @param sd
     * @param ed
     * @return
     */
    public static int subDateDays(Date sd, Date ed) {
        Long eds = ed.getTime();
        Long sds = sd.getTime();
        return (int) ((eds - sds) / MILLION_SECONDS_OF_DAY);
    }

    /**
     * 计算天数差
     *
     * @param sd
     * @param ed
     * @return
     */
    public static int subDateDays(String sd, String ed) {
        Long eds = parse(ed, DATE).getTime();
        Long sds = parse(sd, DATE).getTime();
        return (int) ((eds - sds) / MILLION_SECONDS_OF_DAY);
    }

    /**
     * 取得当月的第一天
     *
     * @return
     */
    public static String getMonthFirst() {
        SimpleDateFormat format = new SimpleDateFormat(DATE);
        Calendar calendar = Calendar.getInstance();//获取当前日期
        calendar.add(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);//设置为1号,当前日期既为本月第一天
        return format.format(calendar.getTime());
    }

    /**
     * 取得当月的最后一天
     *
     * @return
     */
    public static String getMonthLast() {
        SimpleDateFormat format = new SimpleDateFormat(DATE);
        Calendar calendar = Calendar.getInstance();//获取当前日期
//        calendar.add(Calendar.MONTH, 1);
//        calendar.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return format.format(calendar.getTime());
    }

    /**
     * 取得当前日期
     *
     * @return
     */
    public static String getCurrentDate() {
        SimpleDateFormat format = new SimpleDateFormat(DATE);
        Calendar calendar = Calendar.getInstance();//获取当前日期
        return format.format(calendar.getTime());
    }

    /*SELECT * FROM oal_tb_action_logs al WHERE al.action_id = 'use_cabinet'
    AND  al.create_time BETWEEN '2014-11-27 00:00:00' and '2014-11-27 23:59:59'*/

    public static void main(String[] args) {
//        System.out.println(getMonthFirst());
//        System.out.println(getMonthLast());
//        SimpleDateFormat format = new SimpleDateFormat(BOTH);
//        Calendar calendar = Calendar.getInstance();//获取当前日期
//        System.out.println(format.format(calendar.getTime()));
//        List<String> list = null;
//        System.out.println(list.isEmpty());
        String temp = DateHelper.format(new Date(),DateHelper.DATE);
        System.out.println(temp);
    }
}
